package com.da.lon.wang.xlg.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.TakeGoodsChoiseAdapter;
import com.da.lon.wang.xlg.adapter.TakeGoodsChoiseAdapter.CallBack;
import com.da.lon.wang.xlg.bean.OrderInfoResultBean;
import com.da.lon.wang.xlg.bean.OrderInfoResultBean.Str_result;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.util.BuyUtil;
import com.da.lon.wang.xlg.util.DialogUtil;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.WebViewUtil;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class TakeTypeChoiseActivity extends Activity implements CallBack, OnClickListener {
    ListView list;
    TextView title;
    DialogUtil dialogUtil;
    ArrayList<ArrayList<Bitmap>> images;
    ArrayList<Str_result> datas;
    TakeGoodsChoiseAdapter adapter;
    String user_id = PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.USER_ID, "");
    String deviceId = PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.DEVICE_ID, "");
    String user_key = PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.USER_KEY, "");
    BuyUtil util;
    String ids = "";
    String needCoordinates = "";
    private ImageButton topButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_type_choise);
        try {
            list = (ListView) findViewById(R.id.choise_list);
            // 添加列表头必须在列表初始化之后 setAdapter之前
            list.addHeaderView(getHeader());
            title = (TextView) findViewById(R.id.title);
            title.setText("下列商品可自提,请选择自提点");
            images = new ArrayList<ArrayList<Bitmap>>();
            topButton = (ImageButton) findViewById(R.id.ouka_home_buyer_left);
            topButton.setOnClickListener(this);
            datas = new ArrayList<Str_result>();
            dialogUtil = new DialogUtil(this);
            util = new BuyUtil(this);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStart() {
        // 获取数据
        super.onStart();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            ids = bundle.getString("ids");
            needCoordinates = bundle.getString("needCoordinates");
        }
        getStoreInfo(ids, needCoordinates);


    }


    private View getHeader() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.choise_header, null);
        Button choise_take = (Button) v.findViewById(R.id.choise_take);
        choise_take.setOnClickListener(this);
        return v;
    }


    public void getStoreInfo(final String ids, final String needCoordinates) {

        final double[] coordinates = util.getCoordinates();
        MyApplication.getInstance().setLatitude(coordinates[0]);
        MyApplication.getInstance().setLongitude(coordinates[1]);
        if (ids != null && !"".equals(ids)) {
            String url = XLGConstants.URL + XLGConstants.URL_CART_HRAD + XLGConstants.URL_CHOISE;
            HttpLoader<OrderInfoResultBean> loder = new HttpLoader<OrderInfoResultBean>(this, url,
                    OrderInfoResultBean.class);
            AjaxParams params = new AjaxParams();
            params.put("token", XLGDataUtils.encrypt2MD5("Cart" + XLGConstants.SECRET + XLGConstants.URL_CHOISE));
            params.put("id", user_id);
            params.put("ids", ids);
            params.put("needCoordinates", needCoordinates);
            params.put("user_key", user_key);
            params.put("deviceId", deviceId);
            params.put("latitude", coordinates[0] + "");
            params.put("longitude", coordinates[1] + "");
            loder.loadData(params);
            loder.setManagerListener(new OnWebLoadListener<OrderInfoResultBean>() {

                @Override
                public void OnStart() {
                    dialogUtil.showDialog();
                }

                @Override
                public void OnError(String error) {
                    dialogUtil.dismissDialog();
                }

                @Override
                public void onSuccess(OrderInfoResultBean t) {
                    dialogUtil.dismissDialog();
                    if (t != null && "000000".equals(t.getCode())) {
                        datas = t.getResult().getStr_result();
                        if (datas != null) {
                            adapter = new TakeGoodsChoiseAdapter(getApplicationContext(), datas, coordinates,
                                    TakeTypeChoiseActivity.this);
                            list.setAdapter(adapter);
                        }
                    }
                }
            });
        }


    }


    public void showMapChoise() {
        double[] coors = new BuyUtil(this).getCoordinates();
        boolean isBaiduInstall = XLGDataUtils.isInstallApp(this, "com.baidu.BaiduMap");
        boolean isGaodeInstall = XLGDataUtils.isInstallApp(this, "com.autonavi.minimap");
        new WebViewUtil(this).showMapChooseDialog(this, isBaiduInstall, isGaodeInstall, coors[0], coors[1], "目的地");
    }

    @Override
    public void click(View v) {
        switch (v.getId()) {
            case R.id.choise_location:
                // 调用地图
                boolean isBaiduInstall = XLGDataUtils.isInstallApp(TakeTypeChoiseActivity.this, "com.baidu.BaiduMap");
                boolean isGaodeInstall = XLGDataUtils.isInstallApp(TakeTypeChoiseActivity.this, "com.autonavi.minimap");
                if (isBaiduInstall || isGaodeInstall) {
                    new WebViewUtil(TakeTypeChoiseActivity.this).showMapChooseDialog(TakeTypeChoiseActivity.this,
                            isBaiduInstall, isGaodeInstall, Double.parseDouble(datas.get((int) v.getTag()).getLat()),
                            Double.parseDouble(datas.get((int) v.getTag()).getLng()), "目的地");
                } else {
                    Toast.makeText(TakeTypeChoiseActivity.this, "你尚未安装任何第三方地图软件，请先安装！", 2000).show();
                }
                break;
            case R.id.choise_take:
                // 点击到店提货
                Intent intent = new Intent(this, OrderActivity.class);
                int position = (int) v.getTag();
                String data_store = datas.get(position).getStore_id();
                Bundle bundle = new Bundle();
                bundle.putString("ids", ids);
                bundle.putString("data_store", data_store);
                bundle.putString("needCoordinates", needCoordinates);
                bundle.putString("area_type", "1");
                intent.putExtras(bundle);
                MyApplication.addActivity(this);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 后退键
            case R.id.ouka_home_buyer_left:
                finish();
                break;
            case R.id.choise_take:
                // 点击全部邮寄
                Intent intent = new Intent(this, OrderActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("ids", ids);
                bundle.putString("data_store", "-1");
                bundle.putString("needCoordinates", needCoordinates);
                bundle.putString("area_type", "0");
                intent.putExtras(bundle);
                MyApplication.addActivity(this);
                startActivity(intent);
                break;
        }
    }
}
